package com.zhoug.widget.util;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.Toast;


import com.zhoug.common.utils.AppUtils;
import com.zhoug.common.utils.LogUtils;
import com.zhoug.common.utils.StringUtils;
import com.zhoug.widget.R;
import com.zhoug.widget.dialog.list.StringListDialog;

import java.util.Arrays;
import java.util.List;

public class MapSdkManager {
    private static final String TAG = "MapSdkManager";
    private Context context;
    private StringListDialog listDialog;
    private List<String> maps= Arrays.asList("百度地图","高德地图","腾讯地图");

    /**百度地图包名*/
    public static final String PACKAGE_MAP_BAIDU="com.baidu.BaiduMap";
    /**高德地图包名*/
    public static final String PACKAGE_MAP_GAODE="com.autonavi.minimap";
    /**腾讯地图包名*/
    public static final String PACKAGE_MAP_TENCENT="com.tencent.map";



    private String coordType= "bd09ll";

    private double targetLatitude;//终点维度
    private double targetLongitude;//终点经度
    private String targetAddress;//终点地址
    private String targetName;//终点名称

    public MapSdkManager(Context context) {
        this.context = context;
        init();
    }

    private void init(){
        listDialog=new StringListDialog(context, R.style.widgets_dialog_full);
        listDialog.addQuxiaoBtn(true);
        listDialog.setDefWindowAnimations();
        listDialog.setCanceledOnTouchOutside(true);
        listDialog.setData(maps);
        listDialog.setOnItemClickListener((parent, view, position, id) -> {
            listDialog.dismiss();
            switch (position){
                case  0:
                    toBaiduMapRoutePlanning();
                    break;
                case  1:
                    toGaodeMapRoutePlanning();
                    break;
                case  2:
                    toTencentMapRoutePlanning();
                    break;
            }

        });
    }

    /**
     * 调用百度地图路线规划
     * 公交、驾车、步行、骑行路线规划
     */
    private void toBaiduMapRoutePlanning(){
        if(AppUtils.isAppInstalled(PACKAGE_MAP_BAIDU)){
            String uriString="baidumap://map/direction?";
            if(targetLatitude!=0 && targetLongitude!=0){
                uriString+="destination=latlng:"+targetLatitude+","+targetLongitude;
                if(!StringUtils.isEmpty(targetName)){
                    uriString+="|name:"+targetName;
                }
                if(!StringUtils.isEmpty(targetAddress)){
                    uriString+="|addr:"+targetAddress;
                }


            }else{
                 uriString+="name:"+targetName+"|addr:"+targetAddress;
            }
            uriString+="&coord_type="+coordType
                    +"&mode=driving"
                    +"&src="+context.getPackageName();
            LogUtils.d(TAG, "toBaiduMap: uriString="+uriString);

            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.addCategory("android.intent.category.DEFAULT");
            Uri uri= Uri.parse(uriString);
            intent.setData(uri);
            context.startActivity(intent); // 启动调用

        }else{
            Toast.makeText(context, "请先安装百度地图客户端", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 调用高德地图路线规划
     * 公交、驾车、步行、骑行路线规划
     */
    private void toGaodeMapRoutePlanning(){
        if(AppUtils.isAppInstalled(PACKAGE_MAP_GAODE)){
            String uriString="amapuri://route/plan/?";
            if(targetLatitude!=0 && targetLongitude!=0){
                uriString+="dlat="+targetLatitude+"&dlon="+targetLongitude;
                if(targetName!=null){
                    uriString +="&dname"+targetName;
                }
            }else{
                if(targetName!=null){
                    uriString +="&dname"+targetName;
                }
            }

            uriString+="&dev=0"+"&t=0"+"&sourceApplication="+context.getPackageName();
            LogUtils.d(TAG, "toGaodeMapRoutePlanning: uriString="+uriString);

            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setPackage(PACKAGE_MAP_GAODE);
            intent.addCategory("android.intent.category.DEFAULT");
            Uri uri= Uri.parse(uriString);
            intent.setData(uri);
            context.startActivity(intent); // 启动调用

        }else{
            Toast.makeText(context, "请先安装高德地图客户端", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 调用腾讯地图路线规划
     * 公交、驾车、步行、骑行路线规划
     */
    private void toTencentMapRoutePlanning(){
        if(AppUtils.isAppInstalled(PACKAGE_MAP_TENCENT)){
            String uriString="qqmap://map/routeplan?";
            uriString+="type=drive";
            if(targetLatitude!=0 && targetLongitude!=0){
                uriString+="&tocoord="+targetLatitude+","+targetLongitude+"&to="+targetName;
            }else {
                uriString += "&to="+targetName;
            }


            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.addCategory("android.intent.category.DEFAULT");
            Uri uri= Uri.parse(uriString);
            intent.setData(uri);
            context.startActivity(intent); // 启动调用

        }else{
            Toast.makeText(context, "请先安装腾讯地图客户端", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 调用百度地图直接导航,会直接进入导航页面,开启导航
     */
    private void toBaiduMapNavigation(){
        if(AppUtils.isAppInstalled(PACKAGE_MAP_BAIDU)){
            Intent intent = new Intent();
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addCategory("android.intent.category.DEFAULT");
            String uriString="baidumap://map/navi?";
            if(targetLatitude!=0 && targetLongitude!=0){
                uriString +="location="+targetLatitude+","+targetLongitude+"&";
            }
            uriString+="coord_type="+coordType
                    +"&query="+targetAddress
                    +"&src="+context.getPackageName();
            LogUtils.d(TAG, "toBaiduMap: uriString="+uriString);

            Uri uri= Uri.parse(uriString);
            intent.setData(uri);
            context.startActivity(intent); // 启动调用

        }else{
            Toast.makeText(context, "请先安装百度地图客户端", Toast.LENGTH_SHORT).show();
        }
    }




    public void show(){
        listDialog.show();
    }

    public void setTarget(double targetLatitude, double targetLongitude, String targetName, String targetAddress){
        this.targetLatitude=targetLatitude;
        this.targetLongitude=targetLongitude;
        this.targetName=targetName;
        this.targetAddress=targetAddress;
    }

    public String getCoordType() {
        return coordType;
    }

    public void setCoordType(String coordType) {
        this.coordType = coordType;
    }
}
